package com.mall.system.controller;

import com.mall.common.result.Result;
import com.mall.system.service.RoleMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色菜单关联控制器
 */
@Tag(name = "角色菜单关联管理")
@Slf4j
@RestController
@RequestMapping("/system/role-menus")
public class RoleMenuController {
    
    @Autowired
    private RoleMenuService roleMenuService;
    
    @Operation(summary = "根据角色ID获取菜单ID列表")
    @GetMapping("/role/{roleId}")
    public Result<List<Long>> getMenuIdsByRoleId(@PathVariable Long roleId) {
        try {
            List<Long> menuIds = roleMenuService.selectMenuIdsByRoleId(roleId);
            return Result.success(menuIds);
        } catch (Exception e) {
            log.error("根据角色ID获取菜单ID列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "更新角色菜单权限")
    @PutMapping("/role/{roleId}")
    public Result<Boolean> updateRoleMenu(
            @PathVariable Long roleId,
            @RequestBody List<Long> menuIds) {
        try {
            boolean success = roleMenuService.updateRoleMenu(roleId, menuIds);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("更新角色菜单权限失败");
            }
        } catch (Exception e) {
            log.error("更新角色菜单权限异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
}